package com.example.demo.thread.loopPrint;

public class Wait_Notify_ABC {
    private Integer num = 0;
    private static final Object lock = new Object();

    private void printABC(int targetNum){
        for (int i = 0; i < 50; i++) {
            synchronized (lock){
                while(num % 2 != targetNum){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + ":" + num);
                num++;
                lock.notifyAll();
            }
        }
    }

    public static void main(String[] args) {
        Wait_Notify_ABC wait_notify_abc = new Wait_Notify_ABC();
        new Thread(() -> wait_notify_abc.printABC(0), "A").start();
        new Thread(() -> wait_notify_abc.printABC(1), "B").start();
//        new Thread(() -> wait_notify_abc.printABC(2), "C").start();
    }
}
